home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gmp-132.lha / gmp-1.3.2 / tests / tst-dm_ui.c < prev    next >
C/C++ Source or Header  |  1993-05-06  |  3KB  |  117 lines

  1. /* Test mpz_abs, mpz_add, mpz_cmp, mpz_cmp_ui, mpz_div, mpz_div_ui,
  2.    mpz_divmod, mpz_divmod_ui, mpz_mod, mpz_mod_ui, mpz_mul, mpz_mul_ui.
  3.  
  4. Copyright (C) 1993 Free Software Foundation, Inc.
  5.  
  6. This file is part of the GNU MP Library.
  7.  
  8. The GNU MP Library is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. The GNU MP Library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with the GNU MP Library; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include <stdio.h>
  23. #include "gmp.h"
  24. #include "gmp-impl.h"
  25. #include "urandom.h"
  26.  
  27. void debug_mp ();
  28.  
  29. #ifndef SIZE
  30. #define SIZE 8
  31. #endif
  32.  
  33. main (argc, argv)
  34.      int argc;
  35.      char **argv;
  36. {
  37.   MP_INT dividend;
  38.   MP_INT quotient, remainder;
  39.   MP_INT quotient2, remainder2;
  40.   MP_INT temp;
  41.   mp_size dividend_size;
  42.   mp_limb divisor;
  43.   int i;
  44.   int reps = 100000;
  45.  
  46.   if (argc == 2)
  47.      reps = atoi (argv[1]);
  48.  
  49.   mpz_init (÷nd);
  50.   mpz_init ("ient);
  51.   mpz_init (&remainder);
  52.   mpz_init ("ient2);
  53.   mpz_init (&remainder2);
  54.   mpz_init (&temp);
  55.  
  56.   for (i = 0; i < reps; i++)
  57.     {
  58.       dividend_size = urandom () % SIZE - SIZE/2;
  59.       mpz_random2 (÷nd, dividend_size);
  60.  
  61.       divisor = urandom ();
  62.       if (divisor == 0)
  63.     continue;
  64.  
  65.       mpz_divmod_ui ("ient, &remainder, ÷nd, divisor);
  66.       mpz_div_ui ("ient2, ÷nd, divisor);
  67.       mpz_mod_ui (&remainder2, ÷nd, divisor);
  68.  
  69.       /* First determine that the quotients and remainders computed
  70.      with different functions are equal.  */
  71.       if (mpz_cmp ("ient, "ient2) != 0)
  72.     dump_abort (÷nd, divisor);
  73.       if (mpz_cmp (&remainder, &remainder2) != 0)
  74.     dump_abort (÷nd, divisor);
  75.  
  76.       /* Check if the sign of the quotient is correct.  */
  77.       if (mpz_cmp_ui ("ient, 0) != 0)
  78.     if ((mpz_cmp_ui ("ient, 0) < 0)
  79.         != (mpz_cmp_ui (÷nd, 0) < 0))
  80.     dump_abort (÷nd, divisor);
  81.  
  82.       /* Check if the remainder has the same sign as the dividend
  83.      (quotient rounded towards 0).  */
  84.       if (mpz_cmp_ui (&remainder, 0) != 0)
  85.     if ((mpz_cmp_ui (&remainder, 0) < 0) != (mpz_cmp_ui (÷nd, 0) < 0))
  86.       dump_abort (÷nd, divisor);
  87.  
  88.       mpz_mul_ui (&temp, "ient, divisor);
  89.       mpz_add (&temp, &temp, &remainder);
  90.       if (mpz_cmp (&temp, ÷nd) != 0)
  91.     dump_abort (÷nd, divisor);
  92.  
  93.       mpz_abs (&remainder, &remainder);
  94.       if (mpz_cmp_ui (&remainder, divisor) >= 0)
  95.     dump_abort (÷nd, divisor);
  96.     }
  97.  
  98.   exit (0);
  99. }
  100.  
  101. dump_abort (dividend, divisor)
  102.      MP_INT *dividend;
  103.      mp_limb divisor;
  104. {
  105.   fprintf (stderr, "ERROR\n");
  106.   fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
  107.   fprintf (stderr, "divisor  = %lX\n", divisor);
  108.   abort();
  109. }
  110.  
  111. void
  112. debug_mp (x, base)
  113.      MP_INT *x;
  114. {
  115.   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
  116. }
  117.